package com.cff.action;

import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import com.cff.AwtPos;
import com.cff.common.CacheMemory;
import com.cff.common.Constant;
import com.cff.common.SimpleFrame;
import com.cff.mina.skt.MinaUtil;
import com.cff.pos.PosClient;
import com.cff.util.EventUtil;
import com.cff.util.SignTools;

public class SktMsgSendActionListener implements ActionListener {
	SimpleFrame frame;
	Panel controlPanel;
	int colWeith = 200;
	
	JLabel labelMsgType ;
	JTextField textMsgType ;
	JLabel labelTranType ;
	JTextField textTranType ;
	JLabel labelTraceNum ;
	JTextField textTraceNum ;
	JLabel labelLocalTime ;
	JTextField textLocalTime ;
	JLabel labelLocalDate ;
	JTextField textLocalDate ;
	JLabel label41 ;
	JTextField text41 ;
	JLabel label42 ;
	JTextField text42 ;
	
	JLabel label48 ;
	
	JLabel label48_00 ;
	JTextField text48_00 ;
	JLabel label48_08 ;
	JTextField text48_08 ;
	JLabel label48_09 ;
	JTextField text48_09 ;
	JLabel label48_26 ;
	JTextField text48_26 ;
	JLabel label48_44 ;
	JTextField text48_44 ;
	JButton sktButton ;
	
	public SktMsgSendActionListener(Panel controlPanel) {
		super();
		this.controlPanel = controlPanel;
	}

	public SktMsgSendActionListener(SimpleFrame frame,Panel controlPanel) {
		super();
		this.frame = frame;
		this.controlPanel = controlPanel;
	}
	
	public SktMsgSendActionListener(SimpleFrame frame, Panel controlPanel, JLabel labelMsgType,
			JTextField textMsgType, JLabel labelTranType, JTextField textTranType, JLabel labelTraceNum,
			JTextField textTraceNum, JLabel labelLocalTime, JTextField textLocalTime, JLabel labelLocalDate,
			JTextField textLocalDate, JLabel label41, JTextField text41, JLabel label42, JTextField text42,
			JLabel label48, JLabel label48_00, JTextField text48_00, JLabel label48_08, JTextField text48_08,
			JLabel label48_09, JTextField text48_09, JLabel label48_26, JTextField text48_26, JLabel label48_44,
			JTextField text48_44, JButton sktButton) {
		super();
		this.frame = frame;
		this.controlPanel = controlPanel;
		this.labelMsgType = labelMsgType;
		this.textMsgType = textMsgType;
		this.labelTranType = labelTranType;
		this.textTranType = textTranType;
		this.labelTraceNum = labelTraceNum;
		this.textTraceNum = textTraceNum;
		this.labelLocalTime = labelLocalTime;
		this.textLocalTime = textLocalTime;
		this.labelLocalDate = labelLocalDate;
		this.textLocalDate = textLocalDate;
		this.label41 = label41;
		this.text41 = text41;
		this.label42 = label42;
		this.text42 = text42;
		this.label48 = label48;
		this.label48_00 = label48_00;
		this.text48_00 = text48_00;
		this.label48_08 = label48_08;
		this.text48_08 = text48_08;
		this.label48_09 = label48_09;
		this.text48_09 = text48_09;
		this.label48_26 = label48_26;
		this.text48_26 = text48_26;
		this.label48_44 = label48_44;
		this.text48_44 = text48_44;
		this.sktButton = sktButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String msg = "";
		byte bitMap[] = new byte[64];
		String msgType = textMsgType.getText().trim();
		String msg03 = textTranType.getText().trim();
		String msg11 = textTraceNum.getText().trim();
		String msg12 = textLocalTime.getText().trim();
		String msg13 = textLocalDate.getText().trim();
		String msg41 = text41.getText().trim();
		String msg42 = text42.getText().trim();
		String msg48 = "";
		String msg48_00 = text48_00.getText().trim();
		String msg48_08 = text48_08.getText().trim();
		String msg48_09 = text48_09.getText().trim();
		String msg48_44 = text48_26.getText().trim();
		String msg48_26 = text48_44.getText().trim();

		if(!"".equals(msg48_00)){
			msg48 += "00" + String.format("%04d", msg48_00.length()) + msg48_00;
		}
		if(!"".equals(msg48_08)){
			msg48 += "08" + String.format("%04d", msg48_08.length()) + msg48_08;
		}
		if(!"".equals(msg48_09)){
			msg48 += "09" + String.format("%04d", msg48_09.length()) + msg48_09;
		}
		if(!"".equals(msg48_44)){
			msg48 += "44" + String.format("%04d", msg48_44.length()) + msg48_44;
		}
		if(!"".equals(msg48_26)){
			msg48 += "26" + String.format("%04d", msg48_26.length()) + msg48_26;
		}
		if(CacheMemory.sktMsg48.size()> 0){
			Set tmpSet = CacheMemory.sktMsg48.keySet();
			for( Iterator it = tmpSet.iterator(); it.hasNext();){
				String msg48Name = it.next().toString().trim();
				JTextField tmpJTextField = CacheMemory.sktMsg48.get(msg48Name);
				String msg48Filed = tmpJTextField.getText().trim();
				try {
					msg48 += msg48Name + String.format("%04d", msg48Filed.getBytes("GBK").length) + msg48Filed;
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}            
	        } 
		}
		PosClient.printStr(msg48);
		msg48 = String.format("%04d", msg48.length()) + PosClient.StrToHex(msg48);
		PosClient.printStr(msg48);
		if(CacheMemory.sktMsg.size()> 0){
			Set tmpSet = CacheMemory.sktMsg.keySet();
			for( Iterator it = tmpSet.iterator(); it.hasNext();){
				String msgName = it.next().toString().trim();
				JTextField tmpJTextField = CacheMemory.sktMsg.get(msgName);
				String msgFiled = tmpJTextField.getText().trim();
				int msgOrder = Integer.parseInt(msgName);
				if(msgOrder > 0 && msgOrder<3){
					msg += msgFiled;
					bitMap[msgOrder-1] = 1;
				}
	        } 
		}
		msg += msg03;
		if(CacheMemory.sktMsg.size()> 0){
			Set tmpSet = CacheMemory.sktMsg.keySet();
			for( Iterator it = tmpSet.iterator(); it.hasNext();){
				String msgName = it.next().toString().trim();
				JTextField tmpJTextField = CacheMemory.sktMsg.get(msgName);
				String msgFiled = tmpJTextField.getText().trim();
				
				int msgOrder = Integer.parseInt(msgName);
				if(msgOrder > 3 && msgOrder<11){
					msg += msgFiled;
					bitMap[msgOrder-1] = 1;
				}
	        } 
		}
		msg += msg11 + msg12 + msg13 ;
		if(CacheMemory.sktMsg.size()> 0){
			Set tmpSet = CacheMemory.sktMsg.keySet();
			for( Iterator it = tmpSet.iterator(); it.hasNext();){
				String msgName = it.next().toString().trim();
				JTextField tmpJTextField = CacheMemory.sktMsg.get(msgName);
				String msgFiled = tmpJTextField.getText().trim();
				int msgOrder = Integer.parseInt(msgName);
				if(msgOrder > 13 && msgOrder<41){
					msg += msgFiled;
					bitMap[msgOrder-1] = 1;
				}
	        } 
		}
		msg += PosClient.StrToHex(msg41) + PosClient.StrToHex(msg42);
		if(CacheMemory.sktMsg.size()> 0){
			Set tmpSet = CacheMemory.sktMsg.keySet();
			for( Iterator it = tmpSet.iterator(); it.hasNext();){
				String msgName = it.next().toString().trim();
				JTextField tmpJTextField = CacheMemory.sktMsg.get(msgName);
				String msgFiled = tmpJTextField.getText().trim();
				int msgOrder = Integer.parseInt(msgName);
				if(msgOrder > 42 && msgOrder<48){
					msg += msgFiled;
					bitMap[msgOrder-1] = 1;
				}
	        } 
		}
		msg += msg48;
		if(CacheMemory.sktMsg.size()> 0){
			Set tmpSet = CacheMemory.sktMsg.keySet();
			for( Iterator it = tmpSet.iterator(); it.hasNext();){
				String msgName = it.next().toString().trim();
				JTextField tmpJTextField = CacheMemory.sktMsg.get(msgName);
				String msgFiled = tmpJTextField.getText().trim();
				int msgOrder = Integer.parseInt(msgName);
				if(msgOrder > 48 && msgOrder<65){
					msg += msgFiled;
					bitMap[msgOrder-1] = 1;
				}
	        } 
		}
		
		bitMap[3-1] = 1;
		bitMap[11-1] = 1;
		bitMap[12-1] = 1;
		bitMap[13-1] = 1;
		bitMap[41-1] = 1;
		bitMap[42-1] = 1;
		bitMap[48-1] = 1;
		PosClient.printStr(msg);
		PosClient.printBtye(bitMap);
		PosClient.printStr(PosClient.byteToStr(bitMap));
		msg = msgType + PosClient.binaryString2hexString(PosClient.byteToStr(bitMap)) + msg;
		PosClient.printStr(msg);
		try {
			PosClient.sendMsg(msg,AwtPos.sktIp,Integer.parseInt(AwtPos.sktPort.trim()));
			//String result = new PosClient().telnetSend(msg,AwtPos.sktIp,Integer.parseInt(AwtPos.sktPort.trim()));
			//System.out.println(result);
			
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
			
//		byte []msgComplete = PosClient.makeCompleteMsg(msg);
//		MinaUtil mutil = new MinaUtil(AwtPos.sktIp,Integer.parseInt(AwtPos.sktPort.trim()),msgComplete);
	}

	public static void main(String args[]) throws UnsupportedEncodingException{
		String china ="中国12";
		System.out.println(china.length());
		System.out.println(china.getBytes().length);
		System.out.println(china.getBytes("GBK").length);
		System.out.println(china.getBytes("UTF-8").length);
	}
}
